var payment = function(){

    var getPayWay = function(){
        $('.payWay').on('click',function(){
            var way = $(this).find('.am-u-sm-7').html();
            if(way=='微信支付'){
                $(this).find('.am-u-sm-3').removeClass('pay-agent-wechat-un').addClass('pay-agent-wechat');
                $(this).find('i').removeClass('uncheck-icon').addClass('check-icon');
                $(this).siblings('.payWay').find('.am-u-sm-3').removeClass('pay-agent-alipay').addClass('pay-agent-alipay-un');
                $(this).siblings('.payWay').find('i').removeClass('check-icon').addClass('uncheck-icon')
                $('#payChannel').val('wx_pub');
            }else{
                $(this).find('.am-u-sm-3').removeClass('pay-agent-alipay-un').addClass('pay-agent-alipay');
                $(this).find('i').removeClass('uncheck-icon').addClass('check-icon');
                $(this).siblings('.payWay').find('.am-u-sm-3').removeClass('pay-agent-wechat').addClass('pay-agent-wechat-un');
                $(this).siblings('.payWay').find('i').removeClass('check-icon').addClass('uncheck-icon')
                $('#payChannel').val('alipay_wap');
            }

        });
    };

    var payment = function () {
        $('.go-pay').on('click',function(){
            var user = getUserLoginStatus(true);
            console.log(user);
            if(!user){
                doLogin();
                return;
            }
            var  confirmPay= $(this);
            confirmPay.attr('disabled','disabled');
            var payInfo = $.getUrlParam('info');
            var payChannel = $('#payChannel').val();
            if(!payInfo || !payChannel){
                messageBox("获取支付信息失败","alert");
                return;
            }
            $.post('/order/pay',{
                params: payInfo,
                userId:user,
                payChannel:payChannel
            }).then(function (msg) {
                confirmPay.removeAttr('disabled');
                console.log(msg);
                if(msg.code == 200){
                    if(payChannel == 'wx_pub'){
                        $.post('/getWxPubSign',{charge:JSON.stringify(msg.results)}).then(function (msg1) {
                            if(msg1.code == 200){
                                pingpp.createPayment(msg.results, function (results,err) {
                                    if(results == 'success'){
                                        //只有微信支付会返回到这里
                                        $.post('/pay/wxsuccess',{openId:msg1.results,payInfo:payInfo}).then(function (msg2) {
                                            if(msg2.code == 200){
                                                messageBox("支付成功",'alert');
                                                window.location.href = '/orders';
                                            }else{
                                                messageBox("支付成功, 修改订单状态失败",'alert');
                                                $.post('/pay/wxsuccess',{openId:msg1.results,payInfo:payInfo});
                                                window.location.href = '/order';
                                            }
                                        })
                                    }else{
                                        messageBox(result+" "+err.msg+" "+err.extra,'alert');
                                    }
                                },msg1.results,false);
                            }else{
                                messageBox("获取微信公众号签名失败","alert");
                            }
                        })
                    }else{
                        pingpp.setAPURL('http://koudai.lovepinyao.com/public/html/pay.htm');
                        pingpp.createPayment(msg.results, function (results,err) {
                            if(results == 'success'){
                                console.log('支付成功');
                            }else{
                                alert(result+" "+err.msg+" "+err.extra);
                                console.log(result+" "+err.msg+" "+err.extra);
                            }
                        })
                    }
                }else{
                    if(msg.code == 104){
                        //这里得出用户还没有微信登陆过,跳转到微信登陆
                        window.location.href = '/login?redirect=/payment?params='+payInfo;
                    }else{
                        messageBox("获取支付凭证失败","alert");
                    }
                }
            }, function (err) {
                confirmPay.removeAttr('disabled');
                messageBox("获取支付凭证失败",'alert');
            });
        });
    };



    return {
        init:function(){
            getPayWay();
            payment();
        }
    }
}();
